package com.photonpay.api.config;

import com.photonpay.api.response.AccessTokenResponse;

/**
 * 默认配置
 *
 * @author ：lo-
 * @date ：Created in 2023/8/25 15:11
 */
public class DefaultPhotonPayConfig implements PhotonPayConfig {

    protected volatile String appId;
    protected volatile String appSecret;
    protected volatile String serverUrl;
    private volatile String accessToken;
    private volatile String publicKey;
    private volatile String privateKey;

    private volatile long expiresTime;

    /**
     * 获取token
     *
     * @return token
     */
    @Override
    public String getAccessToken() {
        return this.accessToken;
    }

    /**
     * token是否过期
     *
     * @return true = 是
     */
    @Override
    public boolean isAccessTokenExpired() {
        return isExpired(this.expiresTime);
    }

    /**
     * 是否过期
     *
     * @param expiresTime 过期时间
     * @return true = 是
     */
    protected boolean isExpired(long expiresTime) {
        return System.currentTimeMillis() > expiresTime;
    }

    @Override
    public void updateAccessToken(AccessTokenResponse accessToken) {
        AccessTokenResponse.DataInfo data = accessToken.getData();
        updateAccessToken(data.getToken(), data.getExpiresIn());
    }

    /**
     * 更新token
     *
     * @param accessToken      新的accessToken值
     * @param expiresInSeconds 过期时间，以毫秒为单位
     */
    @Override
    public void updateAccessToken(String accessToken, Long expiresInSeconds) {
        this.accessToken = accessToken;
        this.expiresTime = expiresInSeconds;
    }

    @Override
    public String getAppId() {
        return this.appId;
    }

    @Override
    public String getAppSecret() {
        return this.appSecret;
    }

    @Override
    public String getServerUrl() {
        return this.serverUrl;
    }

    @Override
    public String getPublicKey() {
        return this.publicKey;
    }

    @Override
    public String getPrivateKey() {
        return this.privateKey;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }
}
